#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>


#include "../ec_it8733_inc/ec_it8733_dev.h"

#include "ec_it8528_rw.h"

//#define DBG_UART_PRT
//#include "dbg_prt.h"
#include "../ec_it8733_inc/dbg_prt.h"

//static const unsigned char const_u8LdnEnvControl = 0x04;
//static const unsigned char const_u8LdnParallelPort = 0x03;


//static const unsigned char const_u8LdnParall = 0x03;
CEcIt8528Rw* CEcIt8528Rw::m_pInstance = NULL; 

CEcIt8528Rw* CEcIt8528Rw::getInstance()
{
   if (!m_pInstance)   // Only allow one instance of class to be generated.
      m_pInstance = new CEcIt8528Rw;
   return m_pInstance;
}


CEcIt8528Rw::CEcIt8528Rw()
{
  //m_u8PwmDefaultValue = 0;
  m_fd=-1;
	
	m_fd = open("/dev/ec_it8733", O_RDWR);
	if (m_fd < 0)
	{
		//dbg_rn_prt("can't open /dev/ec_it8733 !\n");
		printf("can't open /dev/ec_it8733 !\n");
	}
	dbg_rn_prt("m_fd=%d",m_fd);
  //m_u16BaseAddrLdn04 = 0x0290;
  //m_u16BaseAddrLdnParallelPort = 0;
}

CEcIt8528Rw::~CEcIt8528Rw()
{
	if(m_fd != -1)
    	close(m_fd);
  	m_fd=-1;
}

unsigned char CEcIt8528Rw::ec_inb( unsigned short int port)
{
  return dev_inb( m_fd, port );
}
void CEcIt8528Rw::ec_outb( unsigned char value, unsigned short int port)
{
  dev_outb( m_fd, value, port );
}

#if 0
int CEcIt8528Rw::rtc_read_byte(int nbaseaddr)
{
	dbg_prt("    fggg11\n");
	for(int Index=0;Index<0x0E;Index++)
	{
	ec_outb(Index,nbaseaddr);
	int data=ec_inb(nbaseaddr+1);
	dbg_prt("%02X ", data);
	}
	dbg_prt("    fggg22\n");
}
#endif

int CEcIt8528Rw::pnp_read_byte(int Index)
{
	int data=ec_inb(0x66);
	dbg_rn_prt("0x66=%02X ", data);

#if 0	
	ec_outb(0x07,0x2E);
	ec_outb(Index >> 8,0x2F);

	ec_outb(Index&0xFF,0x2E);
	int data=ec_inb(0x2F);
	#endif
	return 0;
}

int CEcIt8528Rw::pnp_write_byte(int Index,int data)
{

	ec_outb(0x07,0x2E);
	ec_outb(Index >> 8,0x2F);

	
	ec_outb(Index&0xFF,0x2E);

	ec_outb(data,0x2F);
	return 0x00;

}


int CEcIt8528Rw::read_PNPCFG(int nbase_addrpara)
{

	ec_outb(0x07,0x2E);
	ec_outb(nbase_addrpara,0x2F);
	
	for(int i=0;i<256;i++)
	{
		if(i%16==0)
			dbg_rn_prt("");

		ec_outb(i,0x2E);
		int data=ec_inb(0x2F);
		char str[25];
		//itoa(i, str, 16);
		sprintf(str,"%02X ", data );
		dbg_prt("%s",str);
	
	}
	dbg_rn_prt("");
	return 0;
}

int CEcIt8528Rw::ecio_read_byte(unsigned short Index)
{

  int ret = 0x00; 

  ec_outb(0x2e,0x2e);
  ec_outb(0x11,0x2f);
  ec_outb(0x2f,0x2e);
  ec_outb(Index >> 8,0x2f);

  ec_outb(0x2e,0x2e);
  ec_outb(0x10,0x2f);
  ec_outb(0x2f,0x2e);
  ec_outb(Index&0xFF,0x2f);

  ec_outb(0x2e,0x2e);
  ec_outb(0x12,0x2f);
  ec_outb(0x2f,0x2e);
  ret = ec_inb(0x2f);


  //dbg_prt("ret is 0x%x!\n", ret);
  return ret;
}


int CEcIt8528Rw::ecio_write_byte(unsigned short Index,int data)
{

  int ret = 0xff; 

  ec_outb(0x2e,0x2e);
  ec_outb(0x11,0x2f);
  ec_outb(0x2f,0x2e);
  ec_outb((Index >> 8)&0xFF,0x2f);

  ec_outb(0x2e,0x2e);
  ec_outb(0x10,0x2f);
  ec_outb(0x2f,0x2e);
  ec_outb(Index&0xFF,0x2f);

  ec_outb(0x2e,0x2e);
  ec_outb(0x12,0x2f);
  ec_outb(0x2f,0x2e);
  ec_outb(data,0x2f);

//  printk("ret is 0x%x!\n", ret);
  return ret;
}


int CEcIt8528Rw::read_ECIO_RAM(int nbase_addrpara)
{
	unsigned short nbaseaddr=(unsigned short)nbase_addrpara;

	for(int index=0; index<256;index++)
	{
		if(index%16==0)
			dbg_prt("\n");
		unsigned char data=ecio_read_byte(nbaseaddr+index);


			char str[25];
		sprintf(str,"%02X ", data );
		dbg_prt("%02X ", data);	
	}
	dbg_prt("\n");


	return 0;
}